package com.suneee.refund.dao.impl;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.suneee.base.util.ContextHelper;
import com.suneee.orm.GenericDao;
import com.suneee.orm.util.WhereClauseBuilder;
import com.suneee.refund.dao.RefundApplyDao;
import com.suneee.refund.entity.RefundApply;

@Repository("refundApplyDao")
public class RefundApplyDaoImpl extends GenericDao<RefundApply, String>implements RefundApplyDao {

	@Override
	public void updatePhaseTypeByRefundId(String refundId, String status, String feedbackStatus) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_refund_apply set ");
		sql.append("status = ? ,");
		sql.append("feedback_status = ? ");
		sql.append("where refund_id = ?");
		Object[] args = new Object[3];
		args[0] = status;
		args[1] = feedbackStatus;
		args[2] = refundId;
		this.executeUpdate(sql.toString(), args);
	}

	@Override
	public void updateRwRemark(String refundId, String cwRemark) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_refund_apply set ");
		sql.append("cw_remark = ? ");
		sql.append("where refund_id = ?");
		Object[] args = new Object[2];
		args[0] = cwRemark;
		args[1] = refundId;
		this.executeUpdate(sql.toString(), args);
	}

	@Override
	public void updateRefundResult(String refundId, String refundResult) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_refund_apply set ");
		sql.append("refund_result = ? ");
		sql.append("where refund_id = ?");
		Object[] args = new Object[2];
		args[0] = refundResult;
		args[1] = refundId;
		this.executeUpdate(sql.toString(), args);
	}

	@Override
	public void updateRwRemarkByRefuseRemit(String refundId, RefundApply refundApply) {
		StringBuffer sql = new StringBuffer();
		sql.append("update t_refund_apply set ");
		sql.append("cw_refuse_reason = ? ,");
		sql.append("cw_refuse_remark = ? ");
		sql.append("where refund_id = ?");
		Object[] args = new Object[3];
		args[0] = refundApply.getCwRefuseReason();
		args[1] = refundApply.getCwRefuseRemark();
		args[1] = refundId;
		this.executeUpdate(sql.toString(), args);
	}

	@Override
	public List<Map<String, Object>> ConsoleRefundApply(RefundApply refundApply) {
		StringBuffer sql = new StringBuffer(" select distinct re.tid from t_refund_apply  re	left join t_trades_info tr on tr.tid = re.tid where ");
		WhereClauseBuilder sbf = new WhereClauseBuilder();
		sbf.equals("re.process_status",refundApply.getProcessStatus());//处理状态（PROCESS：处理中；INIT：待处理；FAIL：失败；SUCCESS：成功）
//		sbf.equals(" re.process_status, in ('INIT','PROCESS')");
		sbf.equals("re.after_sale_type", refundApply.getAfterSaleType());//售后类型(REFUND_MONEY:退款,REFUND_GOODS:退货,CHANGE_GOODS:换货)
		sbf.equals("re.refund_result", refundApply.getRefundResult());//财务处理结果（INIT:未处理、SUCCESS:退款成功、FAIL：退款失败、ACCEPT：同意退款、DENY：拒绝退款）
		
		//订单待结束退货  拒绝退货/结束退货 ：仓库拒绝入库/仓库同意入库且财务处理同意退款
		if(null != refundApply.getFeedBackList() && refundApply.getFeedBackList().size() > 0)
		{
			sbf.appendWhere(" ((re.status = 'PROCESS' AND re.feedback_status = 'DENY') OR (re.status = 'SUCCESS' AND re.refund_result = 'ACCEPT'))");
		}
		//sbf.in("re.feedback_status", refundApply.getFeedBackList()); //仓库反馈状态
		//sbf.equals("re.is_notice_buyer",refundApply.getIsNoticeBuyer()); //是否通知买家
		sbf.equals("re.is_notice_refund", refundApply.getIsNoticeRefund()); //是否通知财务退款

		sbf.appendWhere(" tr.shop_id in (select shop_id from t_shop_admin where admin_id = "+ContextHelper.getCurrentLoginInfo().getAdminId() +")");
		sbf.equals("tr.enterprise_code",refundApply.getEnterpriseCode());
		sql.append(sbf.getSql());
		
		return this.queryForList(sql.toString(), sbf.getArgs());
	}

	@Override
	public Long getRefundIdStr(String queryStr) {
		// TODO Auto-generated method stub
		String sql = "select count(*) as total from t_refund_apply where refund_id like ? ";
		List<Map<String,Object>> list = this.queryForList(sql, queryStr);
		if(list == null || list.isEmpty())
			return 0L;
		Map<String,Object> map = list.get(0);
		return (Long)map.get("total");
	}

	@Override
	public List<RefundApply> getListByTid(String tid,String skuId) {
		String sql = "select distinct ap.* from t_refund_apply ap,t_trades_order od "
				+ "where substring_index(ap.sku_str,'|',1) = od.sku_id "
				+ "and ap.tid = ? and od.outer_sku_id = ? and ap.process_status != 'DENY' and ap.process_status != 'SUCCESS' ";
		Object[] args = new Object[]{tid,skuId};
		return this.executeQuery(sql, args);
	}

	@Override
	public String getChildAccountToken() {
		// TODO Auto-generated method stub
		String sql = "select session_key from t_child_account where id = ?";
		List<Map<String,Object>> list = this.queryForList(sql, 1);
		if(list == null || list.isEmpty())
			return null;
		Map<String,Object> map = list.get(0);
		return (String)map.get("session_key");
	}

	@Override
	public List<RefundApply> getListByTid(String tid) {
		// TODO Auto-generated method stub
		return this.selectWhere(" tid = ? ", tid);
	}

}
